package au.edu.unsw.infs3634.gamifiedlearning.actiivty;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import org.litepal.LitePal;

import au.edu.unsw.infs3634.gamifiedlearning.MainActivity;
import au.edu.unsw.infs3634.gamifiedlearning.R;
import au.edu.unsw.infs3634.gamifiedlearning.model.UserModel;
import au.edu.unsw.infs3634.gamifiedlearning.utils.SPUtil;

public class LoginActivity extends AppCompatActivity {
    private EditText edtName;
    private EditText edtPwd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        Button btn_login = findViewById(R.id.btn_login);
        TextView tv_register = findViewById(R.id.tv_register);
        TextView tv_reset = findViewById(R.id.tv_reset);
        edtName = findViewById(R.id.edt_name);
        edtPwd = findViewById(R.id.edt_pwd);
        if (!TextUtils.isEmpty(SPUtil.getString(this,"userName",""))){
            edtName.setText(SPUtil.getString(this,"userName",""));
            edtPwd.setText(SPUtil.getString(this,"userPwd",""));
        }
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edtName.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (name.length() > 10) {
                    Toast.makeText(LoginActivity.this, "用户名不得超过10位", Toast.LENGTH_SHORT).show();
                    return;
                }
                String pwd = edtPwd.getText().toString();
                if (TextUtils.isEmpty(pwd)) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (pwd.length() < 6 || pwd.length() > 10) {
                    Toast.makeText(LoginActivity.this, "密码必须在6-10位之间", Toast.LENGTH_SHORT).show();
                    return;
                }
                login(name, pwd);
            }
        });
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });

        tv_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MaterialAlertDialogBuilder(LoginActivity.this)
                        .setTitle("找回密码")
                        .setMessage("将为您找回密码，密码将直接显示到密码输入栏上")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String name = edtName.getText().toString();
                                if (TextUtils.isEmpty(name)) {
                                    Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                UserModel userModel = LitePal.where("name = ?", name).findFirst(UserModel.class);
                                if (userModel == null) {
                                    Toast.makeText(LoginActivity.this, "该用户不存在", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                String pwd = userModel.getPwd();
                                edtPwd.setText(pwd);
                            }
                        })
                        .setNegativeButton("取消",null)
                        .show();
            }
        });
    }

    private void login(String name, String pwd) {
        UserModel userModel = LitePal.where("name = ? and pwd = ?", name, pwd).findFirst(UserModel.class);
        if (userModel == null) {
            Toast.makeText(this, "用户名或者密码错误", Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show();
        finish();
        startActivity(new Intent(this, MainActivity.class));
        SPUtil.saveInt(this , "userId", userModel.getId());
        SPUtil.saveString(this,"userName",name);
        SPUtil.saveString(this,"userPwd",pwd);

    }
}
